"use strict";
/* Controllers */
app.controller("kpiReportListController", ['$scope', '$location', '$rootScope', '$routeParams', 'kpiReportListService', function ($scope, $location, $rootScope, $routeParams, kpiReportListService) {

    var vm = $scope.vm = {};


    vm.p = {};
    vm.result = {};

    vm.p.ownerId = $routeParams.ownerId;
    vm.p.period = $routeParams.period;
    vm.p.dt = $routeParams.dt;

    if(vm.p.dt==null || vm.p.dt=='0') {
        vm.p.dt = getNowFormatDate(-108);
    }
    var errorFn = function (result) {
        alert(result);
    };

    $scope.queryReport = function (t) {
        var vm = $scope.vm;
        $scope.listKanbanReport();
    };

    $scope.listKanbanReport = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        vm.p.param = {
            groupId: vm.p.groupId,
            kpiId: vm.p.kpiId,
            metricId: vm.p.metricId,
            ownerId: vm.p.ownerId,
            dt: vm.p.dt
        };
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        kpiReportListService.listKanbanReport(vm.p.param, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listKanbanReport();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('kpiReportListService', ['ajaxService', function (ajaxService) {

    this.listKanbanReport = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/kpi/listKanbanReport.do", successFunction, errorFunction);
    };

}]);


function reportIdFmt(value, row, index) {
    return [
        '<a href="#/onduty/kpi/kpi_report_add/', value,'" target="_blank">', "<u>修改</u>" , '</a>',
        '<a href="#/onduty/kpi/kpi_report_detail/', row.groupId, '/', row.metricId, '" target="_blank">', " <u>趋势</u>" , '</a>'
    ].join('');
}

function metricIdFmt(value, row, index) {
    return [
        row.metricCn, '（', value, ')'
    ].join('');
}
